#!/usr/bin/env python
# coding: utf-8
from __future__ import unicode_literals
import smtplib
import sys,time
from email.mime.text import MIMEText
from email.header import Header
from email.Utils import COMMASPACE

def sendmail(receiver,subject,mailbody,username='cmccgprs@vip.qq.com',password = 'uzlnaurfhbuebgfi'):
    try:
        smtpserver = 'smtp.qq.com'
        #password = 'uzlnaurfhbuebgfi' #来自QQ邮箱-设置-授权码
        sender = username
        #msg = MIMEText(sys.argv[3],'html','utf-8')
        msg= MIMEText(mailbody,"html","gbk")
        msg['Subject'] = Header(subject, "gbk")
        msg['From'] = username
        msg['To'] = receiver
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.starttls()
        smtp.login(username,password)
        smtp.sendmail(msg['From'], msg['To'],msg.as_string())
        smtp.quit()
        print('%s mail sent !'%time.strftime('%Y-%m-%d %H:%M:%S'))
    except Exception as e:
        print('%s Mail sent failed : %s'%(time.strftime('%Y-%m-%d %H:%M:%S'),e))
if __name__=='__main__':
    if len(sys.argv)>1:
        receiver = sys.argv[1]
        subject = sys.argv[2]
        mailbody = sys.argv[3]
        print receiver
        print subject.decode('gbk')
        print mailbody.decode('gbk')
        sendmail(receiver,subject,mailbody)
    else:
        print('receiver,subject,mailbody could not be null !')



